import fsextra from "fs-extra";
import path from "path";
import { StateType } from "../src/StateType";
import { __dirname } from "./index";
import { savemddatahtml } from "./savemddatahtml";
import { savepagedatajs } from "./savepagedatajs";
import { savepagedatacss } from "./savepagedatacss";

export async function savepagedata(states: StateType) {
    const { menulist, cachetoc, cachetitle, cachemain, cachestylecssrules } =
        states;

    const pagepath = path.resolve(__dirname, "../temp/page");
    await fsextra.ensureDir(pagepath);
    try {
        await fsextra.emptyDir(pagepath);
    } catch (e) {}
    await Promise.all(
        menulist.map(async (url) => {
            await savepagedatajs(url, pagepath, cachetoc, cachetitle);
            await savemddatahtml(url, pagepath, cachemain);
            await savepagedatacss(url, pagepath, cachestylecssrules);
        })
    );
}
